package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.PasswordConstant;
import com.sky.constant.PermissionConstant;
import com.sky.dto.*;
import com.sky.entity.User;
import com.sky.exception.AccountNotFoundException;
import com.sky.exception.PasswordErrorException;
import com.sky.mapper.BackstageMappper;
import com.sky.mapper.UserMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.BackstageService;
import com.sky.service.UserService;
import com.sky.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.List;

/**
 * @author 贾晓晨
 * @version 1.0
 */
@Service
public class BackstageServiceImp implements BackstageService {
    @Autowired
    private BackstageMappper backstageMappper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public User login(UserLoginDTO userLoginDTO) {
        String username = userLoginDTO.getUsername();
        String password = userLoginDTO.getPassword();
        //1、根据用户名查询数据库中的数据
        User user = userMapper.getByUsername(username);
        //2、处理各种异常情况（用户名不存在、密码不对、账号被锁定）
        if (user == null) {
            //账号不存在
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        }

        //密码比对
        // 后期需要进行md5加密，然后再进行比对
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        if (!password.equals(user.getPassword())) {
            //密码错误
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }
        // 判断账号是否是管理员
        if (!password.equals(user.getPassword())) {
            //密码错误
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }
        if(!user.getPermission().equals(PermissionConstant.ADMIN)){
            // 非管理员用户
            throw new PasswordErrorException(MessageConstant.NON_ADMIN_USER);
        }

        //3、返回实体对象
        return user;
    }

    @Override
    public PageResult getArticleList(UserPageQueryDTO userPageQueryDTO) {
        PageHelper.startPage(userPageQueryDTO.getPage(), userPageQueryDTO.getPageSize());
        Page<UserListVO> articleList = backstageMappper.getUserList(userPageQueryDTO);
        long total = articleList.getTotal();
        List records = articleList.getResult();
        return new PageResult(total,records);
    }

    @Override
    public PageResult getDict(DictPageQueryDTO dictPageQueryDTO) {
        PageHelper.startPage(dictPageQueryDTO.getPage(), dictPageQueryDTO.getPageSize());
        Page<DictVO> articleList = backstageMappper.getDict(dictPageQueryDTO);
        long total = articleList.getTotal();
        List records = articleList.getResult();
        return new PageResult(total,records);
    }

    @Override
    public void setUserStatus(UserStatusDTO userStatusDTO) {
        backstageMappper.setUserStatus(userStatusDTO);
    }

    @Override
    public UserListVO getUserInfoById(Integer id) {
        UserListVO userListVO = backstageMappper.getUserInfpById(id);
        return userListVO;
    }

    @Override
    public void updateUserInfo(UserUpdateDTO userUpdateDTO) {
        backstageMappper.updateUserInfo(userUpdateDTO);
    }

    @Override
    public void register(UserBackStageRegisterDTO userBackStageRegisterDTO) {
        if(userBackStageRegisterDTO.getPassword().equals("")){
            userBackStageRegisterDTO.setPassword(
                    DigestUtils.md5DigestAsHex(PasswordConstant.DEFAULT_PASSWORD.getBytes())
            );
        }
        if(userBackStageRegisterDTO.getNickname() == null){
            userBackStageRegisterDTO.setNickname(userBackStageRegisterDTO.getUsername());
        }
        backstageMappper.backStageUserRegister(userBackStageRegisterDTO);
    }

    @Override
    public void deleteUser(List<Integer> ids) {
        backstageMappper.deleteUser(ids);
    }

    @Override
    public DictVO getDictInfo(Integer id) {
        return backstageMappper.getDictInfoById(id);
    }

    @Override
    public void updateDict(BackstageDictUpdate backstageDictUpdate) {
        backstageMappper.updateDict(backstageDictUpdate);
    }

    @Override
    public void addDict(BackstageDictAdd backstageDictAdd) {
        backstageMappper.addDict(backstageDictAdd);
    }

    @Override
    public void updateDictStatusUpdate(BackstageDictStatusUpdate backstageDictStatusUpdate) {
        backstageMappper.updateDictStatusUpdate(backstageDictStatusUpdate);
    }

    @Override
    public PageResult getDictDetails(BackstageDictQueryPage backstageDictQueryPage) {
        PageHelper.startPage(backstageDictQueryPage.getPage(), backstageDictQueryPage.getPageSize());
        Page<DictInfoVO> articleList = backstageMappper.getDictDetailList(backstageDictQueryPage);
        long total = articleList.getTotal();
        List records = articleList.getResult();
        return new PageResult(total,records);
    }

    @Override
    public void addDictDetails(BackstageDictDetailsAdd backstageDictDetailsAdd) {
        backstageMappper.addDictDetails(backstageDictDetailsAdd);
    }

    @Override
    public void updateDictDetails(BackstageDictDetailsUpdateDTO backstageDictDetailsUpdateDTO) {
        backstageMappper.updateDictDetails(backstageDictDetailsUpdateDTO);
    }

    @Override
    public DictInfoVO getDictDetailsById(Integer id) {
        return backstageMappper.getDictDetailsById(id);
    }

    @Override
    public void deleteDictDetail(List<Integer> ids) {
        backstageMappper.deleteDictDetail(ids);
    }

    @Override
    public void deleteDictDetailByDictId(List<Integer> ids) {
        backstageMappper.deleteDictDetailByDictId(ids);
    }

    @Override
    public void deleteDict(List<Integer> ids) {
        backstageMappper.deleteDict(ids);
    }

    @Override
    public List<DictInfoVO> getDictDetailsBydictName(String str) {
        Integer i = backstageMappper.getDictId(str);
        return backstageMappper.getDictDetailsBydictName(i);
    }


    // @Override
    // public DictDetailVO getDictDetails(Integer id) {
    //     List<DictInfoVO> dictInfo = backstageMappper.getDictInfo(id);
    //     return new DictDetailVO().builder().dictName(dictInfo).id(id).build();
    // }


}
